import {
	requestConfig
} from '@/common/config/requestConfig.js' //全局配置参数，包括加密是否开启的开关
import CryptoJS from 'crypto-js'
import AES from 'crypto-js/aes'
import Base64 from 'crypto-js/enc-base64'
import MD5 from 'crypto-js/md5'
import SHA1 from 'crypto-js/sha1'
export default {
	// 加密
	encript(params) {
		// debugger;
		if (params.url) {
			if (params.data && requestConfig.encryptOpen) {
				// console.log('---加密前的入参----：', params.url, params.data)
				var jsonData = JSON.stringify(params.data)
				let aesPrivateKey = requestConfig.AESKey
				var key = CryptoJS.enc.Utf8.parse(aesPrivateKey);
				var srcs = CryptoJS.enc.Utf8.parse(jsonData);
				var encrypted = CryptoJS.AES.encrypt(srcs, key, {
					mode: CryptoJS.mode.ECB,
					padding: CryptoJS.pad.Pkcs7
				});
				//最终请求发送的终极加密参数
				params.data = {
					data: encrypted.toString()
				};
			}
			return params;
		} else {
			console.error('url不能为空~')
		}
	},
	// 解密
	decript(res) {
		// debugger
		if (requestConfig.encryptOpen) {
			// console.log('解密的参数', res)
			let resData = res.data.data;
			let aesPrivateKey = requestConfig.AESKey
			var key = CryptoJS.enc.Utf8.parse(aesPrivateKey);
			let decryptedbytes = CryptoJS.AES.decrypt(resData, key, {
				mode: CryptoJS.mode.ECB,
				padding: CryptoJS.pad.Pkcs7
			});
			var jsonData = CryptoJS.enc.Utf8.stringify(decryptedbytes).toString()
			// console.log('解密的结果', jsonData)
			return JSON.parse(jsonData)
		} else {
			return res.data.data;
		}
	}
};
